<?php
class UserController extends DefaultModuleController {
	public function init() {
		parent::init();
		if(!$this->userId) {
			$this->message('请您登录后操作。', GODHOUSE_DOMAIN_WWW."site-login.htm");
		}
	}
	
	/* 用户中心 */
	public function actionIndex() {
		$lastLoginTime = date('Y-m-d H:i:s', $this->user['lastLoginTime']);
		
		// 用户公告
		$articleList = $this->ArticleModel->findAll("categoryId='".GODHOUSE_ARTICLE_NOTICE_ID."'", 1, 4, "articleId DESC");
		foreach($articleList as &$v) {
			$v['title'] = cutstr($v['title'], '42', '');
		}
		// 订单数
		$orderCount = $this->OrderModel->count("userId='{$this->userId}'");
		
		// 兑换数
		$swapCount = $this->GiftsSwapModel->count("userId='{$this->userId}'");
		
		$this->view->assign('articleList', $articleList);
		$this->view->assign('lastLoginTime', $lastLoginTime);
		$this->view->assign('orderCount', $orderCount);
		$this->view->assign('swapCount', $swapCount);
		$this->display('user_index');
	}
	
	/* 用户资料 */
	public function actionDetail() {
		$userId = $this->userId;
		
		if(isset($_POST['user'])) {
			$user = $_POST['user'];
			$this->UserModel->updateByPk($userId, $user);
			$this->message('修改成功。', GODHOUSE_DOMAIN_WWW."user-detail.htm");
		}
		
		$user = $this->UserModel->findByPk($userId);
		
		$areaList = $this->AreaModel->findAll("fId='1'", 0, 0, "id ASC");
		$subAreaList = $area = array();
		if($user['areaId']) {
			$area = $this->AreaModel->findByPk($user['areaId']);
			if($area) {
				$subAreaList = $this->AreaModel->findAll("fId='".$area['fId']."'");
			}
		}
		
		$this->view->assign('area', $area);
		$this->view->assign('subAreaList', $subAreaList);
		$this->view->assign('areaList', $areaList);
		$this->view->assign('user', $user);
		$this->display('user_detail');
	}
	
	/* 我的兑换 */
	public function actionGifts() {
		$userId = $this->userId;
		
		$page = intval(getgpc('page'));
		$page = max(1, $page);
		
		$sqladd = "userId='$userId'";
		$swapList = $this->GiftsSwapModel->findAll($sqladd, $page, GODHOUSE_PPP3, ' swapId ASC');
		foreach($swapList as &$v) {
			$gifts = $this->GiftsModel->findByPk($v['giftsId']);
			$v['title'] = $gifts['title'];
			$v['statusName'] = Lookup::item('status', $v['status']);
			$v['createdTime'] = date('Y-m-d H:i:s', $v['createdTime']);
		}
		$num = $this->FavoriteModel->count($sqladd);
		$multipage = $this->page($num, GODHOUSE_PPP3, $page, 'user-gifts.htm');
		
		$this->view->assign('swapList', $swapList);
		$this->view->assign('multipage', $multipage);
		$this->display('user_gifts');
	}
	
	/* 我的收藏 */
	public function actionFavorite() {
		$userId = $this->userId;
		
		$page = intval(getgpc('page'));
		$page = max(1, $page);
		$ppp = GODHOUSE_PPP3;
		$sqladd = "userId='$userId'";
		$favoriteList = $this->FavoriteModel->findAll($sqladd, $page, $ppp, ' favoriteId ASC');
		foreach($favoriteList as &$v) {
			$product = $this->ProductModel->findByPk($v['productId']);
			$v['productNo'] = $product['productNo'];
			$category = $this->CategoryModel->findByPk($product['categoryId']);
			$v['cateName'] = $category['cateName'];
			$v['categoryId'] = $product['categoryId'];
			$v['createdTime'] = date('Y-m-d H:i:s', $v['createdTime']);
		}
		$num = $this->FavoriteModel->count($sqladd);
		$multipage = $this->page($num, $ppp, $page, 'user-favorite.htm');
		
		$this->view->assign('favoriteList', $favoriteList);
		$this->view->assign('multipage', $multipage);
		$this->display('user_favorite');
	}
	
	/* 密码修改 */
	public function actionPwd() {
		$userId = $this->userId;
		
		if(getgpc('submitTo', 'P')) {
			$passwd = getgpc('passwd', 'P');
			$newPasswd = getgpc('newPasswd', 'P');
			$newPasswd2 = getgpc('newPasswd2', 'P');
			
			if (!$passwd || !$newPasswd) {
				$this->message('修改的密码不能为空。', 'user-pwd.htm');
			}
			if($newPasswd != $newPasswd2) {
				$this->message('修改密码不一致。', 'user-pwd.htm');
			}
			if($this->user['passwd'] != md5($passwd)) {
				//echo $this->user['passwd'].'|'.md5($passwd);
				$this->message('原密码不正确。', 'user-pwd.htm');
			}
			$this->UserModel->updateByPk($userId, array('passwd'=>md5($newPasswd)));
			$this->message('修改成功。', 'user-pwd.htm');
		}
		$this->display('user_pwd');
	}
} 